home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group93a.txt / 000018_icon-group-sender _Fri Jan 15 11:09:13 1993.msg < prev    next >
Internet Message Format  |  1993-04-21  |  4KB

  1. Received: by cheltenham.cs.arizona.edu; Fri, 15 Jan 1993 06:26:01 MST
  2. Date: Fri, 15 Jan 1993 11:09:13 +0100
  3. From: sperber@soir.informatik.uni-tuebingen.de (Michael Sperber)
  4. Message-Id: <9301151009.AA10873@soir.informatik.uni-tuebingen.de>
  5. To: icon-group@cs.arizona.edu
  6. In-Reply-To: (Steve Wampler)'s message of Wed, 13 Jan 93 18:26:28 +0100 
  7.  <9301131726.AA18608@turing.cse.nau.edu*MAILER@cdc2-gw.rrzn.uni-hannover.de>
  8. Subject: Pattern matching in Icon?
  9. Status: R
  10. Errors-To: icon-group-errors@cs.arizona.edu
  11.  
  12. Steve Wampler writes:
  13. >Ah, I think I fully understand.  I think this would take quite a
  14. >bit of changes to Icon to work - consider that in Icon, 'syntax_tree'
  15. >is just a function call, the user could (in a perverse moment), do
  16. >
  17. >    syntax_tree := f
  18. >
  19. >and the case statement suddenly changes.  I think the problem could
  20. >be resolved in specific instances, but it would greatly complicate the
  21. >case statement:
  22. >
  23. >  (if this function is a record constructor, then don't evaluate it,
  24. >  it's a pattern...)
  25. >
  26. >but I don't see how to do it in general.  I think you would have
  27. >to restrict case clauses to *only* literals and patterns, which is
  28. >more restrictive than now (not that it would likely impact many people).
  29. >
  30. >In the present form, you can still do it by
  31. >building your own set of pattern-matching operations (since the
  32. >runtime system has to have something to do it anyway):
  33. >
  34. >   case s of {
  35. >     match_syntax_tree("let",s) : ...
  36. >     match_syntax_tree("apply",s) :...
  37. >
  38. >which would require you (as the programmer) to establish the pattern-matching
  39. >rules - though to me that's a net win, since I might want to do some form
  40. >of pattern matching that the language implementor didn't anticipate (such
  41. >as some form of 'symantic' pattern matching).
  42.  
  43. The syntactic issue I glitched on was to use the standard case
  44. statement for pattern matching.  How about having a "pattern_case" (or
  45. a "match ... of") or some separate thing, where only patterns are
  46. allowed (meaning: only direct data structure constructors).
  47.  
  48. But I think I still haven't been able to get through fully with my
  49. ideas.  The above construct would not do what I meant.  Also, Bob
  50. Alexander writes:
  51.  
  52. >Is this an Icon-esque paraphrase of the structure you want, or something
  53. >close?  Or do I completely fail to understand the concept?
  54. >
  55. >    case type(s) || "::" || s[1] of { 
  56. >      "syntax_tree::let": {    
  57. >    s[2] := args 
  58. >    ... 
  59. >      } 
  60. >      "syntax_tree::apply": { 
  61. >    s[2] := args 
  62. >    ... 
  63. >    }
  64. >
  65. >(The "::" is an arbitrary separator).  This permanently alters the second
  66. >field of "s" -- if that's not okay it could be cured by working on a
  67. >copy of s.
  68.  
  69. So I'll try yet again:
  70.  
  71. record syntax_tree(operator, arguments)
  72.  
  73. match <expression> of {
  74.   syntax_tree("let", a) : {
  75.   # Now, we get here if <expression> evaluates to a 
  76.   # syntax_tree record with the operator component "let".
  77.   # In this branch, a is bound to the arguments componen
  78.   # of <expression> (NOT the other way around!).
  79.   }
  80.   ...
  81. }
  82.  
  83. In functional languages, this greatly simplifies operating on any kind
  84. of recursive data structures, which syntax trees, say, happen to be.
  85. That is exactly why many people consider {SML, Hope, Miranda} ideally
  86. suited for compiler writing.  Icon does not have this feature, but it
  87. was designed with compiler design in mind.  It is better suited for
  88. the early stages of a compiler, but worse for actual program
  89. transformation and code generation.
  90.  
  91. Cheers :-> Chipsy
  92.